home *** CD-ROM | disk | FTP | other *** search
/ New Star Software Collection / NSS_Collection.iso / 3-004 ms visual basic pro 30 / 4.imz / 4.IMA / OPENDB.FR_ / OPENDB.bin
Text File  |  1993-04-28  |  10KB  |  371 lines

  1. VERSION 2.00
  2. Begin Form fOpenDB 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Open DataBase"
  6.    ClientHeight    =   2160
  7.    ClientLeft      =   2460
  8.    ClientTop       =   3840
  9.    ClientWidth     =   4395
  10.    ControlBox      =   0   'False
  11.    ForeColor       =   &H00C0C0C0&
  12.    Height          =   2565
  13.    Left            =   2400
  14.    LinkTopic       =   "Form2"
  15.    MaxButton       =   0   'False
  16.    MinButton       =   0   'False
  17.    ScaleHeight     =   2119.728
  18.    ScaleMode       =   0  'User
  19.    ScaleWidth      =   4447.084
  20.    Top             =   3495
  21.    Width           =   4515
  22.    Begin ComboBox cDBName 
  23.       BackColor       =   &H00FFFFFF&
  24.       Height          =   300
  25.       Left            =   1680
  26.       Sorted          =   -1  'True
  27.       TabIndex        =   0
  28.       Tag             =   "OL"
  29.       Top             =   105
  30.       Width           =   2655
  31.    End
  32.    Begin TextBox cDataBase 
  33.       BackColor       =   &H00FFFFFF&
  34.       Height          =   285
  35.       Left            =   1680
  36.       TabIndex        =   1
  37.       Tag             =   "OL"
  38.       Top             =   465
  39.       Width           =   2655
  40.    End
  41.    Begin TextBox cUserName 
  42.       BackColor       =   &H00FFFFFF&
  43.       Height          =   285
  44.       Left            =   1680
  45.       TabIndex        =   2
  46.       Tag             =   "OL"
  47.       Top             =   825
  48.       Width           =   2655
  49.    End
  50.    Begin TextBox cPassword 
  51.       BackColor       =   &H00FFFFFF&
  52.       Height          =   285
  53.       Left            =   1680
  54.       PasswordChar    =   "*"
  55.       TabIndex        =   3
  56.       Tag             =   "OL"
  57.       Top             =   1185
  58.       Width           =   2655
  59.    End
  60.    Begin CommandButton OkayButton 
  61.       BackColor       =   &H00C0C0C0&
  62.       Caption         =   "&Open"
  63.       Default         =   -1  'True
  64.       Height          =   375
  65.       Left            =   300
  66.       TabIndex        =   4
  67.       Top             =   1680
  68.       Width           =   1575
  69.    End
  70.    Begin CommandButton CancelButton 
  71.       BackColor       =   &H00C0C0C0&
  72.       Cancel          =   -1  'True
  73.       Caption         =   "&Cancel"
  74.       Height          =   375
  75.       Left            =   2460
  76.       TabIndex        =   5
  77.       Top             =   1680
  78.       Width           =   1575
  79.    End
  80.    Begin Label DataBaseLabel 
  81.       BackColor       =   &H00C0C0C0&
  82.       Caption         =   "DataBase:"
  83.       Height          =   255
  84.       Left            =   120
  85.       TabIndex        =   9
  86.       Top             =   465
  87.       Width           =   1335
  88.    End
  89.    Begin Label DBNameLabel 
  90.       BackColor       =   &H00C0C0C0&
  91.       Caption         =   "Source/Server:"
  92.       Height          =   255
  93.       Left            =   120
  94.       TabIndex        =   6
  95.       Top             =   105
  96.       Width           =   1470
  97.    End
  98.    Begin Label UserNameLabel 
  99.       BackColor       =   &H00C0C0C0&
  100.       Caption         =   "User ID:"
  101.       Height          =   255
  102.       Left            =   120
  103.       TabIndex        =   7
  104.       Top             =   825
  105.       Width           =   1335
  106.    End
  107.    Begin Label PasswordLabel 
  108.       BackColor       =   &H00C0C0C0&
  109.       Caption         =   "Password:"
  110.       Height          =   255
  111.       Left            =   120
  112.       TabIndex        =   8
  113.       Top             =   1170
  114.       Width           =   1335
  115.    End
  116. End
  117. Option Explicit
  118.  
  119. Dim BeenLoaded As Integer
  120.  
  121. 'ODBC.DLL APIs
  122. Declare Function SQLAllocEnv Lib "odbc.dll" (env As Long) As Integer
  123. Declare Function SQLDataSources Lib "ODBC.DLL" (ByVal henv As Long, ByVal fdir As Integer, ByVal szDSN As String, ByVal cbDSNMAx As Integer, pcbDSN As Integer, ByVal szDesc As String, ByVal cbDescMax As Integer, pcbDesc As Integer) As Integer
  124.  
  125. Sub CancelButton_Click ()
  126.   gfDBOpenFlag = False
  127.   Unload Me
  128. End Sub
  129.  
  130. Sub cDBName_Click ()
  131.   On Error Resume Next
  132.  
  133.   Dim tmp As String
  134.   Dim x As Integer
  135.  
  136.   cDataBase = ""
  137.   cUserName = ""
  138.   cPassword = ""
  139.  
  140.   'get the database name if there is one
  141.   tmp = String$(255, 32)
  142.   x = OSGetPrivateProfileString(cDBName, "database", "", tmp, Len(tmp), "ODBC.INI")
  143.   cDataBase = Mid$(tmp, 1, x)
  144.  
  145.   'get the last user name is there is one
  146.   tmp = String$(255, 32)
  147.   x = OSGetPrivateProfileString(cDBName, "lastuser", "", tmp, Len(tmp), "ODBC.INI")
  148.   cUserName = Mid$(tmp, 1, x)
  149.  
  150.   cPassword = ""
  151.  
  152.   If cUserName <> "" Then
  153.     cPassword.SetFocus
  154.   Else
  155.     cDataBase.SetFocus
  156.   End If
  157.  
  158. End Sub
  159.  
  160. Sub Form_Load ()
  161.   Left = (Screen.Width - Width) / 2
  162.   Top = (Screen.Height - Height) / 2
  163.  
  164.   GetDataSources cDBName
  165.  
  166.   MsgBar "Enter DataBase Parameters", False
  167.  
  168.   BeenLoaded = True
  169.  
  170. End Sub
  171.  
  172. Sub Form_Paint ()
  173.   Outlines Me
  174. End Sub
  175.  
  176. Sub Form_Unload (Cancel As Integer)
  177.   MsgBar "", False
  178. End Sub
  179.  
  180. '
  181. 'this routine fills a list box with all available
  182. 'ODBC data sources found in ODBC.INI
  183. '
  184. Sub GetDataSources (listctrl As Control)
  185.   Dim DataSource As String, Description As String
  186.   Dim DataSourceLen As Integer, DescriptionLen As Integer
  187.   Dim retcode As Integer
  188.   Dim henv As Long
  189.  
  190.   If SQLAllocEnv(henv) <> -1 Then
  191.     DataSource = String$(32, 32)
  192.     Description = String$(255, 32)
  193.     'get the first one
  194.     retcode = SQLDataSources(henv, 2, DataSource, Len(DataSource), DataSourceLen, Description, Len(Description), DescriptionLen)
  195.     While retcode = 0 Or retcode = 1
  196.       listctrl.AddItem Mid(DataSource, 1, DataSourceLen)
  197.       DataSource = String$(32, 32)
  198.       Description = String$(255, 32)
  199.       'get all the others
  200.       retcode = SQLDataSources(henv, 1, DataSource, Len(DataSource), DataSourceLen, Description, Len(Description), DescriptionLen)
  201.     Wend
  202.   End If
  203.  
  204. End Sub
  205.  
  206. Sub OkayButton_Click ()
  207.    Dim Connect As String, DataSource As String
  208.    Dim x As Integer
  209.    Dim st As String
  210.    Dim i As Integer
  211.    Dim s As String, t As String
  212.    Dim dbq As String
  213.  
  214.    On Error GoTo OpenError
  215.  
  216.    MsgBar "Opening DataBase", True
  217.  
  218.    If VDMDI.PrefOpenOnStartup.Checked = True Then
  219.      Me.Refresh
  220.    End If
  221.  
  222.    SetHourglass Me
  223.  
  224.    'check for blank server name and clear other parms
  225.    If cDBName = "" Then
  226.      cDataBase = ""
  227.      cUserName = ""
  228.      cPassword = ""
  229.    End If
  230.  
  231.    'build connect string
  232.    Connect = "ODBC;"
  233.    If cUserName <> "" Then
  234.      Connect = Connect + "UID=" + cUserName + ";PWD=" + cPassword
  235.    End If
  236.    If cDataBase <> "" Then
  237.      Connect = Connect + ";DATABASE=" + cDataBase
  238.    End If
  239.     
  240.    'add login timeout
  241.    Connect = Connect + ";LoginTimeout=" & glLoginTimeout
  242.  
  243.    DataSource = cDBName
  244.  
  245.    'save the values
  246.    gstDBName = cDBName
  247.    gstDatabase = cDataBase
  248.    gstUserName = cUserName
  249.    gstPassword = cPassword
  250.    gstDataType = "ODBC"
  251.  
  252.    Me.Hide
  253.    Set gCurrentDB = OpenDatabase(DataSource, False, False, Connect)
  254.    If gfDBOpenFlag = True Then
  255.      CloseAllDynasets
  256.    End If
  257.    gfTransPending = False
  258.    VDMDI.ToolBar.Visible = True
  259.    VDMDI.QueryBuilder.Visible = True
  260.    VDMDI.TblAttach.Visible = False
  261.    fSQL.CreateQueryDefbtn.Visible = False
  262.  
  263.    'process the connect string just in case the
  264.    'values came from the ODBC dialogs
  265.    t = gCurrentDB.Connect
  266.    If InStr(t, "=") Then
  267.      i = 1
  268.      While i <= Len(t) + 1
  269.        If Mid(t, i, 1) = ";" Or i = Len(t) + 1 Then
  270.          If s <> "" And InStr(s, "=") > 0 Then
  271.            Select Case Mid(s, 1, InStr(1, s, "=") - 1)
  272.              Case "DSN"
  273.                gstDBName = Mid(s, InStr(1, s, "=") + 1, Len(s))
  274.              Case "DATABASE"
  275.                gstDatabase = Mid(s, InStr(1, s, "=") + 1, Len(s))
  276.              Case "DBQ"
  277.                gstDatabase = Mid(s, InStr(1, s, "=") + 1, Len(s))
  278.              Case "UID"
  279.                gstUserName = Mid(s, InStr(1, s, "=") + 1, Len(s))
  280.              Case "PWD"
  281.                gstPassword = Mid(s, InStr(1, s, "=") + 1, Len(s))
  282.               Case Else
  283.                'nothing
  284.            End Select
  285.          End If
  286.          s = ""
  287.        Else
  288.          s = s + Mid(t, i, 1)
  289.        End If
  290.        i = i + 1
  291.      Wend
  292.    End If
  293.  
  294.    cDBName = gstDBName
  295.    cDataBase = gstDatabase
  296.    cUserName = gstUserName
  297.    cPassword = gstPassword
  298.  
  299.    x = OSWritePrivateProfileString(gstDBName, "Database", gstDatabase, "ODBC.INI")
  300.    x = OSWritePrivateProfileString(gstDBName, "LastUser", gstUserName, "ODBC.INI")
  301.  
  302.    fTables.Caption = gstDBName + "." + gstDatabase
  303.    gCurrentDB.QueryTimeout = glQueryTimeout
  304.  
  305.    'success
  306.    gfDBOpenFlag = True
  307.  
  308.    ResetMouse Me
  309.    Unload Me
  310.    
  311.    GoTo OkayEnd
  312.  
  313. OpenError:
  314.    ResetMouse Me
  315.    gfDBOpenFlag = False
  316.    If cDBName <> "" Then
  317.      If InStr(1, Error$, "Data source not found") > 0 Then
  318.        Beep
  319.        MsgBox "This DataBase has not been Registered, this will now be attempted for you!", 48
  320.        cDataBase = ""
  321.        cUserName = ""
  322.        cPassword = ""
  323.        If RegisterDB((cDBName)) = True Then
  324.          MsgBox "'" + cDBName + "' has been Registered, proceed with Open.", 48
  325.        End If
  326.      ElseIf InStr(1, Error$, "Login failed") > 0 Then
  327.        Beep
  328.        MsgBox "Invalid Parameter(s), Please try again!", 48
  329.      ElseIf InStr(1, Error$, "QueryTimeout property") > 0 Then
  330.        If glQueryTimeout <> 5 Then
  331.          Beep
  332.          MsgBox "Query Timeout Could not be set, default will be used!", 48
  333.        End If
  334.        Resume Next
  335.      Else
  336.        ShowError
  337.      End If
  338.    End If
  339.  
  340.    MsgBar "Enter DataBase Parameters", False
  341.    Me.Show MODAL
  342.    Resume OkayEnd
  343.  
  344. OkayEnd:
  345.  
  346. End Sub
  347.  
  348. Function RegisterDB (dbname As String) As Integer
  349.    On Error GoTo RDBErr
  350.  
  351.    Dim driver As String
  352.  
  353.    driver = InputBox("Enter Driver Name from ODBCINST.INI File:", "Driver Name", DEFAULTDRIVER)
  354.    If driver <> DEFAULTDRIVER Then
  355.      RegisterDatabase cDBName, driver, False, ""
  356.    Else
  357.      RegisterDatabase cDBName, driver, True, ""
  358.    End If
  359.  
  360.    RegisterDB = True
  361.    GoTo RDBEnd
  362.  
  363. RDBErr:
  364.    RegisterDB = False
  365.    Resume RDBEnd
  366.  
  367. RDBEnd:
  368.  
  369. End Function
  370.  
  371.